﻿@{
    Layout = "~/Views/Shared/_ModalForm.cshtml";
    ConfigDefinition configDefinition = ViewBag.ConfigDefinition;
    string[] attributes = configDefinition.Attributes;
    var attribute = attributes == null ? "特征" : attributes[0];
    var attribute1 = attributes == null ? "特征1" : attributes[1];
    var attribute2 = attributes == null ? "特征2" : attributes[2];
    var hasAttribute = attribute != "Undefined";
    var hasAttribute1 = attribute1 != "Undefined";
    var hasAttribute2 = attribute2 != "Undefined";


    ShowHideOrNone authorizationOption = configDefinition.AuthorizationOption;
    var hasAuthorization = authorizationOption == ShowHideOrNone.Show;
    var hideAuthorization = authorizationOption == ShowHideOrNone.Hide;
    var authorizationValue = configDefinition.AuthorizationValue;
    var hasAuthorizationMark = configDefinition.HasPermissionMark;
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-3 control-label ">父项信息<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="masterId" col="MasterId" type="hidden" />
                <input id="masterInfo" col="MasterInfo" type="text" class="form-control" readonly="readonly" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label ">键<font class="red"> *</font></label>
            <div class="col-sm-8">
                <div>
                    <input id="key" col="Key" type="text" class="form-control" />
                </div>
                <span><i class="fa fa-info-circle"></i> 只能由英文、数字或下划线组成，不超过31个字符</span>
            </div>

        </div>
        @if (hasAttribute)
        {
            <div class="form-group">
                <label class="col-sm-3 control-label ">@(attribute)</label>
                <div class="col-sm-8">
                    <div>
                        <input id="attribute" col="Attribute" type="text" class="form-control" />
                    </div>
                </div>
            </div>
        }
        @if (hasAttribute1)
        {
            <div class="form-group">
                <label class="col-sm-3 control-label ">@(attribute1)</label>
                <div class="col-sm-8">
                    <div>
                        <input id="attribute1" col="Attribute1" type="text" class="form-control" />
                    </div>
                </div>
            </div>
        }
        @if (hasAttribute2)
        {
            <div class="form-group">
                <label class="col-sm-3 control-label ">@(attribute2)</label>
                <div class="col-sm-8">
                    <div>
                        <input id="attribute2" col="Attribute2" type="text" class="form-control" />
                    </div>
                </div>
            </div>
        }

        @{
            if (configDefinition.AuthorizationOption == ShowHideOrNone.Show)
            {
                <div class="form-group">
                    <label class="col-sm-3 control-label ">权限</label>
                    <div class="col-sm-8">
                        <div class="col-sm-4 " id="authorization" col="Authorization"></div>
                    </div>
                </div>
            }
            else if (configDefinition.AuthorizationOption == ShowHideOrNone.Hide)
            {
                <input id="authorization" col="Authorization" type="hidden" />
            }
            if (configDefinition.HasPermissionMark)
            {
                <div class="form-group">
                    <label class="col-sm-3 control-label ">权限标识</label>
                    <div class="col-sm-8">
                        <input id="permissionMark" col="PermissionMark" type="text" class="form-control" />
                    </div>
                </div>}
        }

        <div class="form-group">
            <label class="col-sm-3 control-label ">值</label>
            <div class="col-sm-8">
                @*<input id="value" col="Value" type="text" class="form-control" style="height:50px"/>*@
                <textarea id="value" col="Value" class="form-control" style="height:168px"></textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label ">名称<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="isDefault" col="IsDefault" type="hidden" />
                <input id="name" col="Name" type="text" class="form-control" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label ">显示样式</label>
            <div class="col-sm-8">
                <div id="style" col="Style"></div>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 列表时的显示样式</span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">排序码</label>
            <div class="col-sm-8">
                <input id="sequence" col="Sequence" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">状态</label>
            <div id="status" col="Status" class="col-sm-8"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">描述</label>
            <div class="col-sm-8">
                <textarea id="description" col="Description" class="form-control" style="height:68px"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var masterId = ys.request("masterId");
    var masterInfo = ys.request("masterInfo");

     var act = 'edit';
    if (id == '' | id == null) act = 'add';
    $(function () {
        $("#status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())), default: '@((int)(StatusType.Enabled))' });
        $("#isDefault").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(YesOrNo).EnumToDictionaryJson())), default: '@((int)YesOrNo.No)'  });
        $("#style").ysComboBox({
            data: [{ Key: 'default', Value: '默认' }, { Key: 'primary', Value: '主要' }, { Key: 'success', Value: '成功' }, { Key: 'info', Value: '信息' }, { Key: 'warning', Value: '警告' }, { Key: 'danger', Value: '危险' }],
            class: "form-control"
        });

        if ('@hasAuthorization' == 'True') {
            $('#authorization').ysComboBox({ class: 'no-additional-option',data: ys.getJson(@Html.Raw(typeof(Authorization).EnumToDictionaryJson())) });
        }
        if ('@hideAuthorization' == 'True') {
            //$('#authorization').val = '@authorizationValue';
            //alert('hh');
        }

        getForm();
        //$('#name').val('fff');
        $("#form").validate({
            rules: {
                name: { required: true,maxlength: 63},
                key: { required: true, maxlength: 63 },
                attibute: { maxlength: 63 },
                attibute1: { maxlength: 63 },
                attibute2: { maxlength: 63 },
                permissionMark: { maxlength: 15 },
                style: { required: true, minlength: 3 },

            }
        });
    });

    function getForm() {
        if (act == 'add') {//add
            ys.ajax({
                url: '@Url.Content("~/Sys/ConfigItem/GetMaxSequenceNoJson")' + '?masterId=' + masterId,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var defaultData = {};
                        defaultData.Sequence = rst.Data;
                        defaultData.MasterInfo = masterInfo;
                        defaultData.MasterId = masterId;
                        defaultData.Authorization=@(authorizationValue);
                        defaultData.Style = 'default';
                        $("#form").setWebControls(defaultData);
                    }
                    else ys.msgError(rst.Message);
                }
            });


        }
        else {//edit
            ys.ajax({
                url: '@Url.Content("~/Sys/ConfigItem/GetEditDtoJson")' + '?id=' + id,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        rst.Data.MasterInfo = masterInfo;
                        $("#form").setWebControls(rst.Data);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }

    }

    function saveForm(index) {
        if (!ys.checkTextLenth("value", "值", 0, 5139)) { return; }
        if (!ys.checkTextLenth("description", "描述", 0, 127)) return;

        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            ys.ajax({
                url: '@Url.Content("~/Sys/ConfigItem/")' + act,
                type: "post",
                data: postData,
                success: function (rst) {
                    if (rst.Flag == 1) {
                        if (top.initConfigDetailsData) {
                            top.initConfigDetailsData();
                        }
                        ys.msgSuccess(rst.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(rst.Message);
                    }
                }
            });
        }
    }
    function leaveForm(index) {
        parent.layer.close(index);
    }
</script>

